﻿using Dapper;
using Himp.TaskScheduling.Hangfire.Models;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;
using System.Text;

namespace Himp.TaskScheduling.Hangfire.Jobs
{
    /// <summary>
    /// 水分数据
    /// </summary>
    public class RILS1810ViewMJob : CollectTestDataJob, IHostedService
    {
        #region SQL

        public const string GET_RILS1810ViewM_SQL = @"
            SELECT *
            FROM [dbo].[View_M]
            WHERE CONVERT(VARCHAR(10), [EndDate], 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
            ";

        #endregion

        #region 字段

        private readonly ConnectionStringOption _connectionStrings;
        private ILogger<RILS1810ViewMJob> _logger;

        #endregion

        #region 方法

        public RILS1810ViewMJob(ILogger<RILS1810ViewMJob> logger
            , IOptions<ConnectionStringOption> connectionStrings) : base(logger, connectionStrings)
        {
            _logger = logger;
            _connectionStrings = connectionStrings.Value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("--------启动:机器人化验_水分结果--------");

            try
            {
                using (IDbConnection connection = new SqlConnection(_connectionStrings.KaiY_RILS1810))
                {
                    connection.Open();
                    // 读取数据
                    var chnRecList = connection.Query<RILS1810ViewM>(GET_RILS1810ViewM_SQL);
                    var testProjectID = "200";
                    chnRecList = chnRecList.ToList();

                    foreach (var item in chnRecList)
                    {
                        var existTestCode = ExistTestCode(item.SampleID);
                        var thirdCode = item.SampleID;

                        if (!existTestCode)
                        {
                            continue;
                        }

                        CollectTestDataModel collectTestDataModel = new CollectTestDataModel();
                        collectTestDataModel.thirdCode = thirdCode;

                        if (collectTestDataModel.thirdCode.Length > 11)
                        {
                            collectTestDataModel.thirdCode = collectTestDataModel.thirdCode.Substring(collectTestDataModel.thirdCode.Length - 11);
                        }

                        collectTestDataModel.gh = item.CurrentUser;
                        collectTestDataModel.peopleName = item.CurrentUser;
                        collectTestDataModel.testTime = item.EndDate.ToString("yyyy-MM-dd HH:mm:ss");
                        collectTestDataModel.originalRecordNo = $"RILS1810_M_{item.ID}_{testProjectID}";
                        collectTestDataModel.labID = "1";
                        collectTestDataModel.testProjectID = testProjectID;
                        collectTestDataModel.testDeviceID = "1";
                        collectTestDataModel.unitID = "0";

                        StringBuilder sbTestZBID = new StringBuilder();
                        StringBuilder sbTestValue = new StringBuilder();
                        StringBuilder sbTestDevice = new StringBuilder();

                        if (item.CruWeight != 0) //称量瓶重
                        {
                            sbTestZBID.Append(",202");
                            sbTestValue.Append($",{item.CruWeight}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleWeight != 0) //水分煤样重
                        {
                            sbTestZBID.Append(",203");
                            sbTestValue.Append($",{item.SampleWeight}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.CruWeight != 0 && item.SampleWeight != 0) //总样质量(g)
                        {
                            sbTestZBID.Append(",204");
                            sbTestValue.Append($",{item.CruWeight + item.SampleWeight}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleCalRemain != 0) //烘干后质量(g)
                        {
                            sbTestZBID.Append(",205");
                            sbTestValue.Append($",{item.SampleCalRemain}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleCalRemainW1 != 0) //1次检查性试验后重(g)
                        {
                            sbTestZBID.Append(",206");
                            sbTestValue.Append($",{item.SampleCalRemainW1}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.SampleCalRemainW2 != 0)  //2次检查性试验后重(g)
                        {
                            sbTestZBID.Append(",207");
                            sbTestValue.Append($",{item.SampleCalRemainW2}");
                            sbTestDevice.Append(",11");
                        }
                        if (item.Mad != 0) //Mad
                        {
                            sbTestZBID.Append(",208");
                            sbTestValue.Append($",{item.Mad}");
                            sbTestDevice.Append(",11");
                        }

                        collectTestDataModel.testZBIDs = sbTestZBID.ToString().Length > 0 ? sbTestZBID.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testValues = sbTestValue.ToString().Length > 0 ? sbTestValue.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testDeviceID = sbTestDevice.ToString().Length > 0 ? sbTestDevice.ToString().Substring(1) : string.Empty;

                        Exec(collectTestDataModel);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(exception: e, "异常:机器人化验_水分结果");
            }

            _logger.LogInformation("--------完成:机器人化验_水分结果--------");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        #endregion
    }
}
